/**
 * The MIT License (MIT)
 * Copyright (c) 2021 铭软科技(mingsoft.net)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.mingsoft.msend.service;

import com.alibaba.fastjson.JSONArray;
import net.mingsoft.basic.exception.BusinessException;
import net.mingsoft.basic.util.SpringUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.mdiy.util.ConfigUtil;
import net.mingsoft.msend.biz.ILogBiz;
import net.mingsoft.msend.entity.LogEntity;
import net.mingsoft.msend.entity.TemplateEntity;
import net.mingsoft.msend.util.SendcloudUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service("sendCouldSmsService")
public class SendCouldSmsService extends BaseSendService {
    @Override
    public boolean send(Map<String, String> config, String[] toUser, TemplateEntity template, Map values) {
        ILogBiz logBiz = SpringUtil.getBean(ILogBiz.class);


        String templateSms = template.getTemplateSms();
        if (!StringUtil.isInteger(templateSms)) {
            LOG.error("sendcloud 的模板id不正确");
            return false;
        }

        boolean flag = false;
        for (int i = 0; i < toUser.length; i++) {
            flag = SendcloudUtil.sendSms(
                    config.get("smsUsername"),
                    config.get("smsPassword"),
                    Integer.parseInt(templateSms),
                    "0",
                    toUser[i],
                    JSONArray.toJSONString(values));

            if (flag) {
                LogEntity log = new LogEntity();
                log.setLogType(SMS);
                log.setTemplateId(Integer.parseInt(template.getId()));
                log.setLogDatetime(new Date());
                log.setLogContent(JSONArray.toJSONString(values));
                log.setLogReceive(toUser[i]);
                logBiz.save(log);
            } else {
                LOG.error("发送失败：" + toUser[i]);
                break;
            }
        }
        return flag;
    }
}
